package idpa.paul.hangman.model;

public class hangmanModel {

	
	//variables
	private String word;
	private String match;
	private String guessedLetters;
	private int numberFailedGuesses;
	private int numberDiscovered;
	private boolean discovered[];
	
	
	
	//constructor
	public hangmanModel(String word){
		guessedLetters = "";
		this.word = word;
		this.match = word.toLowerCase();
		this.numberFailedGuesses = 0;
		this.numberDiscovered = 0;
		discovered = new boolean[word.length()];
		for(int i=0; i<discovered.length; i++){
			
			discovered[i]=false;
			
		}
		
		
	}
	
	
	
	//methods
	
	//returns number of letters in word
	
	public int length(){
		return word.length();
		
		
	}
	
	//returns the guessed letters list
	
	public String getGuessedLetters(){
		
		return guessedLetters;
		
	}
	
	
	//determines if a letter at a specific index has been discovered return true if discovered false otherwise
	
	public boolean isDiscovered(int i){
		return discovered[i];
		
	}
	
	//gets a letter at an index if that letter has been correctly guessed returns letter i if correctly guessed
	
	public String getLetter(int i){
		if(discovered[i]){
			return Character.toString(word.charAt(i));
			
		}else{
			
			return "_";
			
		}
		
		
	}
	
	
	//determines if a letter is in the word. updates discovered array with all matches. increments number failed guesses if no matches
	//char c is guessed letter
	//true if correct. false otherwise
	public boolean guessLetter(char c){
		boolean correctguess = false;
		guessedLetters+=", "+c;
		for(int i = 0; i<word.length(); i++){
			
			if(Character.toLowerCase(c)==match.charAt(i)){
				
				correctguess = true;
				discovered[i]=true;
			}
			
			
		}
		if(correctguess != true){
			
			numberFailedGuesses+=1;
			
		}
		
		return correctguess;
	}
	
	//return number failed guesses
	public int getNumberFailedGuesses(){
		
		return numberFailedGuesses;
		
		
	}
	
	//returns number of revealed letters
	public int countDiscovered(){
		
		numberDiscovered=0;
		
		for(int i = 0; i<word.length(); i++){
			
			if(discovered[i]==true){
				
				numberDiscovered+=1;
				
				
				
			}
			else{
				
				
			}
			
		}
		return numberDiscovered;
		
	}
	public void initialize(String word){
		guessedLetters = "";
		this.word = word;
		this.match = word.toLowerCase();
		this.numberFailedGuesses = 0;
		this.numberDiscovered = 0;
		discovered = new boolean[word.length()];
		for(int i=0; i<discovered.length; i++){
			
			discovered[i]=false;
			
		}
		
	}
	
	
	
	
	
	
	
}
